package com.lemon.bean;

import javafx.collections.ObservableList;
import javafx.scene.control.TreeItem;
import com.lemon.util.IConUtil;

import java.io.File;

public class MyTreeItem extends TreeItem<String> {
    private boolean isFirstClick = true;
    private File file;
    public MyTreeItem(File file){
        super(IConUtil.getFileName(file),IConUtil.getFileIconToNode(file));
        this.file=file;
    }
    public MyTreeItem(){

    }
    @Override
    public ObservableList<TreeItem<String>> getChildren() {
        ObservableList<TreeItem<String>> children = super.getChildren();
        if(file!=null&&isFirstClick&&this.isExpanded()) {
            isFirstClick=false;
            if(file.listFiles()!=null){
                for(File item:file.listFiles()){
                    if(item.isDirectory()&&!item.getName().startsWith("$")){
                        MyTreeItem myTreeItem = new MyTreeItem(item);
                        children.add(myTreeItem);
                    }
                }
            }
        }
        return children;
    }
    @Override
    public boolean isLeaf() {
        if(file==null){
            return false;
        }
        if(file.isFile()){
            return true;
        }

        return false;
    }
    public File getFile() {
        return file;
    }
}
